const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // apipics: 'http://139.129.55.172:92',
    num: 1,
    showTip: false,
    typeid: 1,
    scroll: 0,
    page: 1,
    pagesize: 24,
    typelist: [],
    prolist: [],
    list: [],
    scope: 0,
    pageindex: 1,
    scope: [0, 0],
    img: app.globalData.img
  },
  typexiang: function (e) {
    // console.log(e.currentTarget.dataset.index.ProductId)
    wx: wx.navigateTo({
      url: "/pages/prodetail/prodetail?id=" + e.currentTarget.dataset.index.ProductId,
    })
  },

  showTip: function () {
    var that = this
    wx.showLoading({
      title: '加载中',
    })

    setTimeout(function () {
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease'
      });
      animation.translate(0, -220).step()
      that.setData({
        showTip: true,
      })
      setTimeout(function () {
        that.setData({
          ani: animation.export()
        })
      }, 1)
      wx.hideLoading()
    }, 1000)
  },
  closeTip: function (e) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        showTip: false,
        ani: animation.export()
      })
    }

  },
  // 购物车减加
  sub: function () {
    if (this.data.num <= 1) {
      this.setData({
        num: 1
      })
    } else {
      this.setData({
        num: this.data.num - 1
      })
    }
  },
  add: function () {
    this.setData({
      num: this.data.num + 1
    })
  },
  // 点击确定加入购物车 弹框
  pushcard: function (e) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        showTip: false,
        ani: animation.export()
      })
    }
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    })
    this.setData({
      num: 1
    })
  },

  producType: function () {
    var that = this
    app.req({
      url: 'Product/Type/Product',
      data: {
        pageindex: that.data.page,
        pagesize: that.data.pagesize
      },
      success: function (res) {
        that.setData({
          proList: res.data,
          // list: res.data[0].Result[0].Product.Price
        })
      }
    })


  },
  scrollList(e) {
    this.setData({
      typeid: e.currentTarget.dataset.type,
      scroll: 9999999999
    })
  },
  scroll(e) {
    // console.log(e)
    var scrollnum = e.detail.scrollTop
    this.setData({
      scroll: scrollnum,
      typeid: 0
    })
    // console.log(this.data.typeid, this.data.scroll)
  },

  // gettype() {
  //   app.req({
  //     url: "Product/ProductType",
  //     success(res) {

  //     }
  //   })
  // },
  getlist() {
    var that = this
    wx.showLoading()
    app.req({
      url: "Product/ProductType",
      success(res1) {
        var a={}
          a.ProductPubTypeId=9999999999,
          a.ProductPubTypeName= "热门单品"
          res1.data.Result.unshift(a)


        var arr = res1.data.Result
        // console.log(arr)
        var typelist = []
        var prolist = []
        var scope = 0
        app.req({
          url: "Product/Type/Product",
          data: {
            pageindex: that.data.pageindex,
            pagesize: 99999
          },
          success(res) {
            for (let i = 0; i < arr.length; i++) {
              var arr2 = []
              for (let j = 0; j < res.data.Result.length; j++) {
                if (arr[i].ProductPubTypeId == res.data.Result[j].ProductPubTypeId) {
                  arr2.push(res.data.Result[j])
                }
                
              }
              if (arr2.length > 0) {
                var obj = {}
                obj.ProductPubTypeId = arr[i].ProductPubTypeId
                obj.name = arr[i].ProductPubTypeName
                obj.list = arr2
                var small = that.data.scope[1]
                var big = that.data.scope[1] + arr2.length * 100 + 25
                var scope = [+small, +big]
                that.setData({
                  scope: scope
                })
                // console.log(small, big, that.data.scope)
                arr[i].scope = scope
                typelist.push(arr[i])
                prolist.push(obj)
                that.setData({
                  typelist: that.data.typelist.concat(arr[i]),
                  prolist: that.data.prolist.concat(obj)
                })
                that.setData({
                  typeid: that.data.typelist[0].ProductPubTypeId
                })
              }
              if (i == arr.length - 1) {
                wx.hideLoading()
              }
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.producType()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})